﻿using NewLife.Agent;
using NewLife.Log;
using Nito.AsyncEx;
using Quartz;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZYing.QPanel.Service.Demo;

public class QuartzService : ServiceBase
{
    public QuartzService()
    {
        this.ServiceName = nameof(QuartzService);
        this.DisplayName = nameof(QuartzService);
        this.Description = "ZYing.QPanel 演示服务";
    }
    private IScheduler? _scheduler;
    private QPanelHttpServer? _httpServer;
    protected override void StartWork(string reason)
    {
        _scheduler = AsyncContext.Run(() => Quartz.Impl.StdSchedulerFactory.GetDefaultScheduler());
        _scheduler.Start();
        _httpServer = new QPanelHttpServer(_scheduler)
        {
            Port = 8080,
            Log = XTrace.Log,
            SessionLog = XTrace.Log
        };
        _httpServer.Start();
        base.StartWork(reason);
    }

    protected override void StopWork(string reason)
    {
        if (_scheduler != null) AsyncContext.Run(() => _scheduler.Shutdown());
        if (_httpServer != null) _httpServer.Stop(reason);
        base.StopWork(reason);
    }
}
